package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 第一个参与计算的数值
     */
    private String num1;
    private boolean isSign = false;
    /**
     * 预算符
     */
    private String sign;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        text = findViewById(R.id.result);

        findViewById(R.id.button_ce).setOnClickListener(this);
        findViewById(R.id.button_divide).setOnClickListener(this);
        findViewById(R.id.button_multi).setOnClickListener(this);
        findViewById(R.id.button_c).setOnClickListener(this);
        findViewById(R.id.button_7).setOnClickListener(this);
        findViewById(R.id.button_8).setOnClickListener(this);
        findViewById(R.id.button_9).setOnClickListener(this);
        findViewById(R.id.button_add).setOnClickListener(this);
        findViewById(R.id.button_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        CharSequence oldText = text.getText();
        Integer inputNum = null;
        String oldTextValue = "";
        if (oldText != null) {
            oldTextValue = oldText.toString();
        }
        if (v.getId() == R.id.button_ce) {
            num1 = null;
            sign = null;
            text.setText("");
        } else if (v.getId() == R.id.button_divide) {
            sign = "÷";
            num1 = text.getText().toString();
            text.setText(sign);
            isSign = true;
        } else if (v.getId() == R.id.button_multi) {
            sign = "x";
            num1 = text.getText().toString();
            text.setText(sign);
            isSign = true;
        } else if (v.getId() == R.id.button_c) {
            text.setText("");
        } else if (v.getId() == R.id.button_7) {
            inputNum = 7;
        } else if (v.getId() == R.id.button_8) {
            inputNum = 8;
            text.setText(oldTextValue + "7");
        } else if (v.getId() == R.id.button_9) {
            inputNum = 9;
        } else if (v.getId() == R.id.button_equal) {
            if (num1 == null || num1.isEmpty()) {
                num1 = "0";
            }
            String num2 = "";
            CharSequence num2Chars = text.getText();
            if (num2Chars != null && num2Chars.length() > 0) {
                num2 = num2Chars.toString();
            }
            Float num1Value = Float.valueOf(num1);
            Float num2Value = Float.valueOf(num2);
            Float result = null;
            if ("÷".equals(sign)) {
                result = num1Value / num2Value;
            } else if ("x".equals(sign)) {
                result = num1Value * num2Value;
            }
            text.setText(String.valueOf(result));
        }

        if (inputNum != null) {
            if (isSign) {
                text.setText(String.valueOf(inputNum));
            } else {
                text.setText(oldTextValue + inputNum);
            }
            isSign = false;
        }
    }
}